//
//  UIViewController+UIViewController_HUD.m
//  MallManage_IOS
//
//  Created by 张浩 on 2023/9/11.
//

#import "UIViewController+HUD.h"

#import "MBProgressHUD.h"
#import <objc/runtime.h>

static const void *HttpRequestHUDKey = &HttpRequestHUDKey;

@implementation UIViewController (HUD)

- (MBProgressHUD *)HUD {
    
    return objc_getAssociatedObject(self, HttpRequestHUDKey);
}

- (void)setHUD:(MBProgressHUD *)HUD{
    
    objc_setAssociatedObject(self, HttpRequestHUDKey, HUD, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)showHudInView:(UIView *)view hint:(NSString *)hint{
    
    if (self.HUD) {
        
        [self hideHud];
    }
    
    //重新初始化
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:view];
    HUD.label.text = hint;
    [view addSubview:HUD];
    [HUD showAnimated:YES];
    [self setHUD:HUD];
    
    [self bringNavToFront];
}

/**
 *  自定义图片的提示，3s后自动消息
 *
 *  @param text 要显示的文字
 *  @param icon 图片地址(建议不要太大的图片)
 *  @param view 要添加的view
 */
- (void)showCustomIcon:(NSString *)iconName Title:(NSString *)title ToView:(UIView *)view {
    
    if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = title;
    hud.label.font = FONT(15);
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:iconName]];
    
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 代表需要蒙版效果
//    hud.dimBackground = YES;
    
    // 3秒之后再消失
    [hud hideAnimated:YES afterDelay:2];
    
}

- (void)showError:(NSString *)error ToView:(UIView *)view {
    
    [self showCustomIcon:@"MBHUD_Error" Title:error ToView:view];
}

- (void)showSuccess:(NSString *)success ToView:(UIView *)view {
    
    [self showCustomIcon:@"MBHUD_Success" Title:success ToView:view];
}

- (void)showInfo:(NSString *)Info ToView:(UIView *)view {
    
    [self showCustomIcon:@"MBHUD_Info" Title:Info ToView:view];
}

- (void)showWarn:(NSString *)Warn ToView:(UIView *)view {
    
    [self showCustomIcon:@"MBHUD_Warn" Title:Warn ToView:view];
}

/* 把导航 放到 最上面 **/
- (void)bringNavToFront {
    
    for (UIView * subView in self.view.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"WXCustomTitleView")]) {
            [self.view bringSubviewToFront:subView];
            break;
        }
    }
}

- (void)showStateText:(NSString *)hint {
    
    //显示提示信息
    UIView *view = [[UIApplication sharedApplication].delegate window];
    //    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    [view addSubview:hud];
    hud.userInteractionEnabled = NO;
    //  [hud showAnimated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabel.text = hint;
    //    hud.labelText = hint;
    hud.margin = 10.f;
    //    hud.yOffset = IS_IPHONE_5?200.f:150.f;
    hud.offset = CGPointMake(0, (view.MM_h - hud.MM_h) / 2);
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:1.5];
    
}

- (void)hideHud {
    
    [[self HUD] hideAnimated:YES];
}
@end
